
#include <BImage>
#include <BStyle>
#include <BPainter>
#include <BLabelItem>
#include <BReadWrite>
#include <BFileDialog>
#include <BSonDocument>

#include "CrossLineItem.h"
#include "GameView.h"

GameView::GameView(BWidget* parent) : BGraphicWidget(parent)
{
	_polygonItem = new PolygonItem();
	_polygonItem->setPos(200);
	_polygonItem->setSize(300);
	//_polygonItem->setVisible(false);
	this->addItem(_polygonItem);
	_buttonLayout = new BHBoxLayout();
	{
		_loadButton = new BButton("load");
		_saveButton = new BButton("save");
		_resetButton = new BButton("reset");
		_splitButton = new BButton("split");
		_buttonLayout->addWidget(_loadButton);
		_buttonLayout->addWidget(_saveButton);
		_buttonLayout->addSpring();
		_buttonLayout->addWidget(_resetButton);
		_buttonLayout->addWidget(_splitButton);
	}
	this->addLayout(_buttonLayout);

	//BLabelItem* labelItem = new BLabelItem();
	//{
	//	BGlyph* graphic = ReadFile<BGlyph>(u8"data/svgs/promo-code.svg");
	//	graphic->setSize(graphic->size() * 5);
	//
	//	labelItem->setImage(graphic);
	//	labelItem->setImageSize(-1);
	//	labelItem->setSize(graphic->size());
	//}
	//this->addItem(labelItem);

	CrossLineItem* crossLine = new CrossLineItem();
	crossLine->setRect(600, 200, 300, 300);
	this->addItem(crossLine);

	connect(this, Signal_Freshed, &GameView::slotViewFlushed);
	connect(_loadButton, Signal_Clicked, &GameView::slotLoadButtonClicked);
	connect(_saveButton, Signal_Clicked, &GameView::slotSaveButtonClicked);
	connect(_resetButton, Signal_Clicked, &GameView::slotResetButtonClicked);
	connect(_splitButton, Signal_Clicked, &GameView::slotSplitButtonClicked);
}
GameView::~GameView()
{

}

void GameView::slotViewFlushed()
{
	const BRectf& itemRect = _polygonItem->globalRect();
	BRect rect = _buttonLayout->sizeHint();
	rect.setPos(itemRect.leftBottom());
	rect.setWidth((int)itemRect.width());
	_buttonLayout->setRect(rect);
	_buttonLayout->dirty();
}
void GameView::slotLoadButtonClicked()
{
	const BString& fileName = BFileDialog::OpenFile(this, "load polygon file", ".son");
	if (fileName.size())
	{
		BSonDocument document;
		if (BSonNode* node = document.load(fileName))
		{
			const BString& text = node->text();
			BStringArray segments = text.split(';');
			for (int i = 0; i < segments.size(); i++)
			{
				const BString& segment = segments[i];
				BStringArray words = segment.split(',');
				float x = words[0].toFloat();
				float y = words[1].toFloat();
				_polygonItem->appendPoint(BPointf(x, y));
			}
		}
	}
}
void GameView::slotSaveButtonClicked()
{
	const BString& fileName = BFileDialog::SaveFile(this, "save polygon file", ".son");
	if (fileName.size())
	{
		BSonDocument document;
		if (BSonNode* node = document.create("polygon"))
		{
			BString text;
			for (int i = 0; i < _polygonItem->points().size(); i++)
			{
				const BPointf& point = _polygonItem->points().at(i);
				text << point.x() << ',' << point.y() << ';';
			}
			text -= ';';
			node->setText(text);
		}
		document.save(fileName);
	}
}
void GameView::slotResetButtonClicked()
{
	_polygonItem->clearPoints();
	_polygonItem->clearPolygons();
}
void GameView::slotSplitButtonClicked()
{
	_polygonItem->splitPolygons();
}

void GameView::styleEvent(const BEvent& event)
{
	BGraphicWidget::styleEvent(event);
	const BStyle* style = event.value();
	if (BImage* image = style->image(Image_Area))
	{
		this->setAreaSize(image->size());
	}
}
void GameView::freshEvent(const BEvent& event)
{
	BGraphicWidget::freshEvent(event);
}
void GameView::paintEvent(const BEvent& event)
{
	BGraphicWidget::paintEvent(event);
}
